package org.avapir.snake;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * 
 * @author Alpen Ditrix
 *
 */
public class KeyInputHandler extends KeyAdapter implements Constants{

		@Override
		public void keyPressed(KeyEvent e) {
			if(debugMode)
			Debug.key = e;
			Snake snake1 = Game.snakes.get(0), snake2 = Game.snakes.get(1); 
			
			switch (e.getKeyCode()) {
			/* snake1 */
			case KeyEvent.VK_RIGHT:
				if (snake1.getDirection() != Direction.LEFT)
					snake1.setDirection(Direction.RIGHT);
				break;
			case KeyEvent.VK_UP:
				if (snake1.getDirection() != Direction.DOWN)
					snake1.setDirection(Direction.UP);
				break;
			case KeyEvent.VK_LEFT:
				if (snake1.getDirection() != Direction.RIGHT)
					snake1.setDirection(Direction.LEFT);
				break;
			case KeyEvent.VK_DOWN:
				if (snake1.getDirection() != Direction.UP)
					snake1.setDirection(Direction.DOWN);
				break;
			case KeyEvent.VK_SPACE:
				snake1.setDirection(Direction.STOP);
				snake2.setDirection(Direction.STOP);
				break;
			/* snake2 */
			case KeyEvent.VK_D:
				if (snake2.getDirection() != Direction.LEFT)
					snake2.setDirection(Direction.RIGHT);
				break;
			case KeyEvent.VK_W:
				if (snake2.getDirection() != Direction.DOWN)
					snake2.setDirection(Direction.UP);
				break;
			case KeyEvent.VK_A:
				if (snake2.getDirection() != Direction.RIGHT)
					snake2.setDirection(Direction.LEFT);
				break;
			case KeyEvent.VK_S:
				if (snake2.getDirection() != Direction.UP)
					snake2.setDirection(Direction.DOWN);
				break;
			case KeyEvent.VK_EQUALS:
				Game.SPS += 5;
				break;
			case KeyEvent.VK_MINUS:
					if (Game.SPS > 5)
						Game.SPS -= 5;
					break;
			case KeyEvent.VK_ESCAPE:
				System.exit(0);
				break;
			case KeyEvent.VK_1:
				if(debugMode){
					Game.stop(1);
				}
				break;
			case KeyEvent.VK_2:
				if(debugMode){
					Game.stop(2);
				}
				break;
			case KeyEvent.VK_R:
				if (!Game.isRunning()) {
					Game.restart();
				}
				break;
			default:
				if (!Game.isRunning()) {
					System.out.println("Destroying application...");
					System.exit(0);
				}
				break;
			}
		}

		// public void keyReleased(KeyEvent e) {
		// switch (e.getKeyCode()) {
		// case KeyEvent.VK_RIGHT:
		// direction[0] = false;
		// break;
		// case KeyEvent.VK_UP:
		// direction[1] = false;
		// break;
		// case KeyEvent.VK_LEFT:
		// direction[2] = false;
		// break;
		// case KeyEvent.VK_DOWN:
		// direction[3] = false;
		// break;
		// }
		// }
	}
